package com.limit.control.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.rj.util.security.MD5;

import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheKit;
import com.limit.constant.Constant;
import com.limit.model.Actor;
import com.limit.model.ActorModule;
import com.limit.model.Admin;
import com.limit.model.Module;
import com.limit.utils.StringUtil;

public class LoginController extends Controller {
	
	public void index(){
		this.setAttr("basePath", this.getRequest().getContextPath());
		String username = StringUtil.valueOf(this.getSessionAttr(Constant.UserName));
		String actorId = StringUtil.valueOf(this.getSessionAttr(Constant.ActorId));
		System.out.println(username+"  "+actorId);
		if(null == username || null==actorId){
			String loginMessage = this.getSessionAttr("loginMessage");
			if(null==loginMessage){
				this.setAttr("loginMessage", "");
			}else{
				this.setAttr("loginMessage", loginMessage);
			}
			this.removeSessionAttr("loginMessage");
			this.render("login.html");
		}else{
			this.redirect("/admin");
		}
	}
	
	public void login(){
		String username = this.getPara("username");
		String password = this.getPara("password");
		
		if(null==username || null==password){
			this.setSessionAttr("loginMessage", "用户名或密码不能为空");
			this.redirect("/login/index");
		}else{
			username = username.trim();
			password = password.trim();
			if(username.isEmpty() || password.isEmpty()){
				this.setSessionAttr("loginMessage", "用户名或密码不能为空");
				this.redirect("/login/index");
			}
			System.out.println("username and password ok");
			Admin admin = this._validateLoginInfo(username,password);
			if(null == admin){
				this.setSessionAttr("loginMessage", "用户名或密码错误");
				this.redirect("/login/index");
			}else{
				long actorId = admin.getLong("actor_id");
				this._cacheUserPerimissionList(actorId,username);
//				this._saveLoginInfo(actorId);
				System.out.println("login data ok");
				this.removeSessionAttr("loginMessage");
				this.setSessionAttr(Constant.AdminId, admin.getLong("id"));
				this.setSessionAttr(Constant.ActorId, actorId);
				this.setSessionAttr(Constant.UserName, username);
				this.redirect("/admin/index");
			}
		}
	}
	
	public void logout(){
		this.removeSessionAttr(Constant.ActorId);
		this.removeSessionAttr(Constant.UserName);
		this.removeSessionAttr(Constant.AdminId);
		this.redirect("/login/index");
	}
	
	/**
	 * 缓存当前登陆用户角色权限列表。
	 */
	private void _cacheUserPerimissionList(long actorId,String username){
		List<Map<String,Object>> actorPerimissionList = new ArrayList<Map<String,Object>>();
		Actor actor = Actor.dao.findById(actorId);
		if(null!=actor){
			List<ActorModule> umlist = actor.getAdminModuleList();
			if(null!=umlist && umlist.size()>0){
				for(ActorModule um : umlist){
					Map<String,Object> map = new HashMap<String,Object>();
					Module m = um.getModule();
					map.put("id", m.get("id"));
					map.put("pid", m.get("pid"));
					map.put("url", m.get("url"));
					map.put("name", m.get("menu_name"));
					actorPerimissionList.add(map);
				}
				CacheKit.put(Constant.CacheActorPermission, username, actorPerimissionList);
			}
		}
	}

	/**
	 * 检测登陆账号是否存在，若存在返回管理员actor_id
	 */
	private Admin _validateLoginInfo(String username,String password){
		password = MD5.UTF8Encode(password);
//		System.out.println("sql "+ username + password);
		Admin admin = Admin.dao.findFirst("select * from admin where username=? and password=? and disable=?",username,password,-1);
		return admin;
	}
	
	private void _saveLoginInfo(long userId){
		
	}
}
